Spring Boot এবং gRPC Client Integration

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client)
97
97

gRPC (Google Remote Procedure Call) হলো একটি আধুনিক RPC (Remote Procedure Call) ফ্রেমওয়ার্ক যা দ্রুত, স্কেলেবল এবং কম লেটেন্সি যোগাযোগের জন্য ডিজাইন করা হয়েছে। এটি HTTP/2 প্রোটোকল এবং Protocol Buffers (Protobuf) ব্যবহার করে ডেটা সংক্ষেপন এবং কার্যকর যোগাযোগ নিশ্চিত করে।

Spring Boot এর মাধ্যমে gRPC ক্লায়েন্ট তৈরি এবং ব্যবহার করা যায়, যা মাইক্রোসার্ভিস আর্কিটেকচারের জন্য কার্যকর।


Spring Boot এবং gRPC Client Integration করার ধাপসমূহ

1. প্রয়োজনীয় ডিপেনডেন্সি যোগ করা

Maven বা Gradle-এ gRPC এবং Protobuf প্লাগইন যোগ করতে হবে।

Maven:

<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty-shaded</artifactId>
    <version>1.58.0</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-protobuf</artifactId>
    <version>1.58.0</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-stub</artifactId>
    <version>1.58.0</version>
</dependency>
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.24.0</version>
</dependency>

Gradle:

implementation 'io.grpc:grpc-netty-shaded:1.58.0'
implementation 'io.grpc:grpc-protobuf:1.58.0'
implementation 'io.grpc:grpc-stub:1.58.0'
implementation 'com.google.protobuf:protobuf-java:3.24.0'

2. Protocol Buffers (Protobuf) ডিফিনেশন তৈরি করা

একটি .proto ফাইল তৈরি করুন। উদাহরণস্বরূপ, hello.proto:

syntax = "proto3";

option java_package = "com.example.grpc";
option java_outer_classname = "HelloProto";

service HelloService {
  rpc sayHello (HelloRequest) returns (HelloResponse);
}

message HelloRequest {
  string name = 1;
}

message HelloResponse {
  string message = 1;
}

3. Protobuf ফাইল থেকে কোড জেনারেট করা

Gradle-এ protobuf plugin যোগ করুন:

plugins {
    id 'com.google.protobuf' version '0.9.3'
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.24.0"
    }
    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:1.58.0"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                grpc {}
            }
        }
    }
}

sourceSets {
    main {
        java {
            srcDirs 'build/generated/source/proto/main/java'
            srcDirs 'build/generated/source/proto/main/grpc'
        }
    }
}

কমান্ড চালান:

./gradlew build

এই কমান্ডটি Protobuf ফাইল থেকে Java ক্লাস জেনারেট করবে।


4. gRPC Client তৈরি করা

একটি Spring Boot ক্লাস তৈরি করে gRPC সার্ভার কল করার জন্য ক্লায়েন্ট তৈরি করুন:

import com.example.grpc.HelloProto.HelloRequest;
import com.example.grpc.HelloProto.HelloResponse;
import com.example.grpc.HelloServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.stereotype.Service;

@Service
public class GrpcClientService {

    private final HelloServiceGrpc.HelloServiceBlockingStub stub;

    public GrpcClientService() {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9090)
                                                      .usePlaintext()
                                                      .build();
        stub = HelloServiceGrpc.newBlockingStub(channel);
    }

    public String sendMessage(String name) {
        HelloRequest request = HelloRequest.newBuilder()
                                           .setName(name)
                                           .build();
        HelloResponse response = stub.sayHello(request);
        return response.getMessage();
    }
}

5. Controller এর মাধ্যমে gRPC ক্লায়েন্ট ব্যবহার করা

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GrpcClientController {

    private final GrpcClientService grpcClientService;

    public GrpcClientController(GrpcClientService grpcClientService) {
        this.grpcClientService = grpcClientService;
    }

    @GetMapping("/grpc/hello")
    public String getHelloMessage(@RequestParam String name) {
        return grpcClientService.sendMessage(name);
    }
}

6. gRPC সার্ভার তৈরি করা (যদি প্রয়োজন হয়)

import com.example.grpc.HelloProto.HelloRequest;
import com.example.grpc.HelloProto.HelloResponse;
import com.example.grpc.HelloServiceGrpc;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;

@GRpcService
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {

    @Override
    public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
        String message = "Hello, " + request.getName();
        HelloResponse response = HelloResponse.newBuilder()
                                              .setMessage(message)
                                              .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}

gRPC ক্লায়েন্ট এর জন্য Key Points

  1. HTTP/2 সমর্থন: gRPC HTTP/2 প্রোটোকল ব্যবহার করে, যা ডেটা ট্রান্সফারকে আরও দ্রুত করে।
  2. Protocol Buffers: ডেটা সংক্ষেপণের জন্য Protobuf ব্যবহার করা হয়।
  3. Blocking এবং Non-blocking Stub: gRPC ক্লায়েন্টের জন্য Blocking এবং Non-blocking stub উভয় ধরনের সমর্থন রয়েছে।
  4. Security: gRPC TLS/SSL সমর্থন করে, যা যোগাযোগকে আরও সুরক্ষিত করে।

উপসংহার

Spring Boot এবং gRPC Client Integration একটি দ্রুত এবং স্কেলেবল সিস্টেম তৈরি করতে সহায়তা করে। এটি মাইক্রোসার্ভিস আর্কিটেকচারের জন্য একটি চমৎকার সমাধান, যেখানে কম লেটেন্সি এবং উচ্চ পারফরম্যান্সের প্রয়োজন। নতুন প্রজেক্টে gRPC ব্যবহার করলে Spring Boot এর সঙ্গে এটি সহজেই ইন্টিগ্রেট করা যায়।

Content added By

gRPC কি এবং Spring Boot এর মধ্যে এর ব্যবহার

67
67

gRPC (Google Remote Procedure Call) হলো একটি উচ্চ-পারফরম্যান্স, ওপেন-সোর্স RPC (Remote Procedure Call) ফ্রেমওয়ার্ক। এটি HTTP/2 ব্যবহার করে মাইক্রোসার্ভিসগুলোর মধ্যে দ্রুত এবং কার্যকর ডেটা আদান-প্রদান নিশ্চিত করে। gRPC মূলত Protocol Buffers (Protobuf) ব্যবহার করে ডেটা সিরিয়ালাইজেশন সম্পন্ন করে, যা JSON বা XML-এর তুলনায় অনেক দ্রুত।


gRPC-এর প্রধান বৈশিষ্ট্য

  1. উচ্চ-পারফরম্যান্স:
    • HTTP/2 ব্যবহার করে দ্রুত ডেটা আদান-প্রদান নিশ্চিত করে।
  2. বাইনারি ফরম্যাট:
    • Protobuf এর মাধ্যমে কম ডেটা সাইজ এবং কম্প্যাক্ট ফরম্যাট।
  3. স্ট্রিমিং সাপোর্ট:
    • ক্লায়েন্ট-টু-সার্ভার এবং সার্ভার-টু-ক্লায়েন্ট উভয় স্ট্রিমিং সমর্থন করে।
  4. মাল্টি-ল্যাঙ্গুয়েজ সাপোর্ট:
    • Java, Python, Go, Node.js সহ আরও অনেক প্রোগ্রামিং ভাষা সমর্থিত।
  5. সিকিউরিটি:
    • TLS/SSL এর মাধ্যমে সুরক্ষিত যোগাযোগ।

Spring Boot-এর মধ্যে gRPC-এর ব্যবহার

১. প্রজেক্ট সেটআপ

Maven ডিপেন্ডেন্সি যোগ করা
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty</artifactId>
    <version>1.57.0</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-protobuf</artifactId>
    <version>1.57.0</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-stub</artifactId>
    <version>1.57.0</version>
</dependency>
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.24.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
Gradle ডিপেন্ডেন্সি
implementation 'io.grpc:grpc-netty:1.57.0'
implementation 'io.grpc:grpc-protobuf:1.57.0'
implementation 'io.grpc:grpc-stub:1.57.0'
implementation 'com.google.protobuf:protobuf-java:3.24.0'
implementation 'org.springframework.boot:spring-boot-starter'

২. Protocol Buffers (.proto) ফাইল তৈরি করা

একটি .proto ফাইল তৈরি করুন, যেখানে সার্ভিস এবং মেসেজ ডিফাইন করা থাকবে।

syntax = "proto3";

option java_package = "com.example.grpc";
option java_outer_classname = "HelloServiceProto";

service HelloService {
  rpc sayHello (HelloRequest) returns (HelloResponse);
}

message HelloRequest {
  string name = 1;
}

message HelloResponse {
  string message = 1;
}
Protobuf Compiler ইনস্টলেশন (protoc):
  • Protobuf ফাইল কম্পাইল করতে protoc ব্যবহার করুন।
  • Maven বা Gradle বিল্ড টুল স্বয়ংক্রিয়ভাবে .proto ফাইল কম্পাইল করতে পারে।

৩. Protobuf কম্পাইল এবং জেনারেটেড ক্লাস

Maven-এর মাধ্যমে .proto ফাইল কম্পাইল:
<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.6.2</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <configuration>
                <protocExecutable>/path/to/protoc</protocExecutable>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

কম্পাইল করার পরে HelloServiceGrpc এবং অন্যান্য ক্লাস জেনারেট হবে।


৪. gRPC সার্ভার তৈরি করা

HelloService Implementation:
import com.example.grpc.HelloServiceGrpc;
import com.example.grpc.HelloServiceProto.HelloRequest;
import com.example.grpc.HelloServiceProto.HelloResponse;
import io.grpc.stub.StreamObserver;
import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
    @Override
    public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
        String name = request.getName();
        String message = "Hello, " + name + "!";

        HelloResponse response = HelloResponse.newBuilder()
                .setMessage(message)
                .build();

        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}
gRPC সার্ভার চালু করা:
import io.grpc.Server;
import io.grpc.ServerBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class GrpcServer {

    private final HelloServiceImpl helloService;

    public GrpcServer(HelloServiceImpl helloService) {
        this.helloService = helloService;
    }

    @PostConstruct
    public void startServer() throws Exception {
        Server server = ServerBuilder.forPort(8080)
                .addService(helloService)
                .build()
                .start();

        System.out.println("gRPC Server is running...");
        server.awaitTermination();
    }
}

৫. gRPC ক্লায়েন্ট তৈরি করা

HelloService Client:
import com.example.grpc.HelloServiceGrpc;
import com.example.grpc.HelloServiceProto.HelloRequest;
import com.example.grpc.HelloServiceProto.HelloResponse;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.stereotype.Component;

@Component
public class HelloServiceClient {

    private final HelloServiceGrpc.HelloServiceBlockingStub blockingStub;

    public HelloServiceClient() {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
                .usePlaintext()
                .build();

        this.blockingStub = HelloServiceGrpc.newBlockingStub(channel);
    }

    public String sayHello(String name) {
        HelloRequest request = HelloRequest.newBuilder()
                .setName(name)
                .build();

        HelloResponse response = blockingStub.sayHello(request);
        return response.getMessage();
    }
}
ক্লায়েন্ট ব্যবহার করা:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class GrpcClientRunner implements CommandLineRunner {

    private final HelloServiceClient helloServiceClient;

    public GrpcClientRunner(HelloServiceClient helloServiceClient) {
        this.helloServiceClient = helloServiceClient;
    }

    @Override
    public void run(String... args) throws Exception {
        String response = helloServiceClient.sayHello("John");
        System.out.println("Response from gRPC server: " + response);
    }
}

gRPC-এর সুবিধা

  1. উচ্চ পারফরম্যান্স:
    • HTTP/2 এবং বাইনারি ডেটা সিরিয়ালাইজেশন (Protobuf) ব্যবহারে দ্রুত যোগাযোগ।
  2. স্ট্রিমিং সাপোর্ট:
    • ক্লায়েন্ট ও সার্ভারের মধ্যে স্ট্রিমিং ডেটা প্রসেসিং।
  3. মাল্টি-ল্যাঙ্গুয়েজ:
    • মাইক্রোসার্ভিস আর্কিটেকচারে ভিন্ন ভাষায় ডেভেলপ করা সার্ভিসগুলোর মধ্যে যোগাযোগ।
  4. সিকিউর:
    • TLS/SSL এর মাধ্যমে সুরক্ষিত ডেটা আদান-প্রদান।

উপসংহার

gRPC স্প্রিং বুটের সাথে ইন্টিগ্রেট করলে উচ্চ-পারফরম্যান্স, লো-লেটেন্সি, এবং ইফিসিয়েন্ট মাইক্রোসার্ভিস যোগাযোগ নিশ্চিত করা যায়। এটি বিশেষ করে রিয়েল-টাইম ডেটা অ্যাপ্লিকেশন, ভিডিও স্ট্রিমিং, এবং মাইক্রোসার্ভিস কমিউনিকেশনের জন্য কার্যকর।

Content added By

Spring Boot এ gRPC Client তৈরি করা

112
112

gRPC একটি উচ্চ-প্রদর্শন মাইক্রোসার্ভিস RPC (Remote Procedure Call) ফ্রেমওয়ার্ক, যা Protobuf ব্যবহার করে ডেটা সিরিয়ালাইজেশন করে। Spring Boot-এ gRPC Client তৈরি করার ধাপ নিচে দেয়া হলো।


1. প্রয়োজনীয় ডিপেন্ডেন্সি যোগ করা

Spring Boot প্রজেক্টে gRPC ব্যবহার করতে grpc-spring-boot-starter ডিপেন্ডেন্সি যোগ করতে হবে।

Maven:

<dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-spring-boot-starter</artifactId>
    <version>2.14.0</version>
</dependency>

Gradle:

implementation 'net.devh:grpc-spring-boot-starter:2.14.0'

2. Proto ফাইল তৈরি করা

gRPC সেবা ব্যবহারের জন্য .proto ফাইল তৈরি করুন। নিচে একটি উদাহরণ দেয়া হলো:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.example.grpc";
option java_outer_classname = "GreetingProto";

service GreetingService {
    rpc Greet (GreetingRequest) returns (GreetingResponse);
}

message GreetingRequest {
    string name = 1;
}

message GreetingResponse {
    string message = 1;
}

3. Proto ফাইল কম্পাইল করা

Proto ফাইল থেকে Java ক্লাস জেনারেট করতে protobuf-maven-plugin ব্যবহার করুন।

Maven Plugin Configuration:

<plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <version>0.6.1</version>
    <configuration>
        <protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact>
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.54.0:exe:${os.detected.classifier}</pluginArtifact>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>compile-custom</goal>
            </goals>
        </execution>
    </executions>
</plugin>

mvn clean install রান করলে Java ফাইলগুলো জেনারেট হবে।


4. gRPC Client তৈরি করা

Spring Boot-এ gRPC ক্লায়েন্ট তৈরি করতে আমরা @GrpcClient এনোটেশন ব্যবহার করব।

import com.example.grpc.GreetingServiceGrpc;
import com.example.grpc.GreetingProto.GreetingRequest;
import com.example.grpc.GreetingProto.GreetingResponse;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

@Service
public class GreetingClient {

    @GrpcClient("greeting-service")
    private GreetingServiceGrpc.GreetingServiceBlockingStub greetingServiceStub;

    public String getGreeting(String name) {
        GreetingRequest request = GreetingRequest.newBuilder()
                .setName(name)
                .build();
        GreetingResponse response = greetingServiceStub.greet(request);
        return response.getMessage();
    }
}

5. gRPC সার্ভারের সাথে যোগাযোগ

@RestController বা অন্য কোনও কম্পোনেন্ট থেকে gRPC ক্লায়েন্ট ব্যবহার করতে পারেন।

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private final GreetingClient greetingClient;

    public GreetingController(GreetingClient greetingClient) {
        this.greetingClient = greetingClient;
    }

    @GetMapping("/greet")
    public String greet(@RequestParam String name) {
        return greetingClient.getGreeting(name);
    }
}

6. gRPC সার্ভার ঠিকানা কনফিগার করা

application.yml বা application.properties ফাইলে gRPC সার্ভারের ঠিকানা যোগ করুন:

grpc:
  client:
    greeting-service:
      address: "localhost:6565"

7. Spring Boot অ্যাপ্লিকেশন চালানো

Spring Boot অ্যাপ্লিকেশন চালান এবং /greet?name=John URL-এ অ্যাক্সেস করুন। এটি gRPC সার্ভারে একটি রিকুয়েস্ট পাঠাবে এবং সার্ভারের রেসপন্স রিটার্ন করবে।


সংক্ষেপে:

  1. gRPC প্রটোকল ব্যবহার করে Spring Boot-এ উচ্চ-দক্ষতাসম্পন্ন ক্লায়েন্ট তৈরি করা সম্ভব।
  2. Protobuf ব্যবহার করে ডেটা ডিফাইন ও সিরিয়ালাইজ করা হয়।
  3. gRPC ক্লায়েন্ট ইমপ্লিমেন্টেশনে Spring Boot এবং grpc-spring-boot-starter সহজ সমাধান দেয়।

এটি ব্যবহার করে আপনার মাইক্রোসার্ভিস স্থাপনার কার্যকারিতা বাড়ানো সম্ভব।

Content added By

Protobuf ফাইল তৈরি এবং gRPC এর মাধ্যমে ডেটা পাঠানো

60
60

gRPC হলো একটি উচ্চ পারফরম্যান্স RPC (Remote Procedure Call) ফ্রেমওয়ার্ক, যা Google দ্বারা তৈরি। এটি ডেটা আদান-প্রদানের জন্য Protobuf (Protocol Buffers) ফাইল ব্যবহার করে। gRPC HTTP/2 প্রোটোকল ব্যবহার করে এবং এটি বিভিন্ন প্ল্যাটফর্ম এবং ভাষায় সমর্থিত।


gRPC এর মূল উপাদান

  1. Protobuf:
    • একটি ডেটা সিরিয়ালাইজেশন ফরম্যাট যা gRPC মেসেজ প্রোটোকল হিসেবে কাজ করে।
  2. Service Definition:
    • gRPC সার্ভিসের অপারেশন Protobuf ফাইলে সংজ্ঞায়িত করা হয়।
  3. Client-Server Communication:
    • gRPC ব্যবহার করে ক্লায়েন্ট এবং সার্ভারের মধ্যে যোগাযোগ করা হয়।

gRPC সেটআপ এবং Spring Boot ক্লায়েন্ট তৈরি করার ধাপ

Step 1: প্রয়োজনীয় ডিপেন্ডেন্সি যোগ করা

pom.xml-এ gRPC এবং Protobuf-এর জন্য ডিপেন্ডেন্সি যোগ করুন:

<dependencies>
    <!-- gRPC Starter -->
    <dependency>
        <groupId>net.devh</groupId>
        <artifactId>grpc-spring-boot-starter</artifactId>
        <version>2.13.1</version>
    </dependency>
    
    <!-- Protobuf -->
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.21.12</version>
    </dependency>
    
    <!-- gRPC Java -->
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-netty-shaded</artifactId>
        <version>1.56.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
        <version>1.56.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
        <version>1.56.0</version>
    </dependency>
</dependencies>

Step 2: Protobuf ফাইল তৈরি করা

Protobuf ফাইলটি gRPC সার্ভিস এবং মেসেজ গঠন সংজ্ঞায়িত করতে ব্যবহৃত হয়। উদাহরণস্বরূপ, user.proto:

syntax = "proto3";

option java_package = "com.example.grpc";
option java_outer_classname = "UserProto";

service UserService {
    rpc GetUser (UserRequest) returns (UserResponse);
}

message UserRequest {
    int32 id = 1;
}

message UserResponse {
    int32 id = 1;
    string name = 2;
    string email = 3;
}

Step 3: Protobuf জেনারেটর প্লাগইন যোগ করা

Maven-এ Protobuf ফাইল জেনারেট করতে নিচের প্লাগইন যোগ করুন:

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.7.0</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <configuration>
                <protocExecutable>/path/to/protoc</protocExecutable>
                <pluginId>grpc-java</pluginId>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note: Ensure protoc ইনস্টল করা আছে।


Step 4: gRPC সার্ভার তৈরি করা

gRPC সার্ভারে Protobuf অনুযায়ী সার্ভিস ইমপ্লিমেন্ট করুন:

import com.example.grpc.UserProto.UserRequest;
import com.example.grpc.UserProto.UserResponse;
import com.example.grpc.UserServiceGrpc;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

@GrpcService
public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {

    @Override
    public void getUser(UserRequest request, StreamObserver<UserResponse> responseObserver) {
        UserResponse response = UserResponse.newBuilder()
                .setId(request.getId())
                .setName("John Doe")
                .setEmail("johndoe@example.com")
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}

Step 5: gRPC ক্লায়েন্ট তৈরি করা

gRPC ক্লায়েন্ট থেকে সার্ভারে কল করতে নিচের মতো কোড লিখুন:

import com.example.grpc.UserProto.UserRequest;
import com.example.grpc.UserProto.UserResponse;
import com.example.grpc.UserServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.stereotype.Service;

@Service
public class UserClient {

    private final UserServiceGrpc.UserServiceBlockingStub userServiceStub;

    public UserClient() {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9090)
                .usePlaintext()
                .build();
        userServiceStub = UserServiceGrpc.newBlockingStub(channel);
    }

    public UserResponse getUser(int id) {
        UserRequest request = UserRequest.newBuilder().setId(id).build();
        return userServiceStub.getUser(request);
    }
}

Step 6: ক্লায়েন্ট কল করা

Spring Boot অ্যাপ্লিকেশন চালিয়ে gRPC কল করুন:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements CommandLineRunner {

    @Autowired
    private UserClient userClient;

    @Override
    public void run(String... args) {
        System.out.println("Calling gRPC Server...");
        var response = userClient.getUser(1);
        System.out.println("Response: " + response.getName() + ", " + response.getEmail());
    }
}

gRPC এর কার্যপ্রণালী

  1. Protobuf থেকে জেনারেট করা Java কোড:
    • Protobuf ফাইল থেকে মেসেজ ও সার্ভিসের জন্য Java ক্লাস তৈরি হয়।
  2. Server-Side Implementation:
    • gRPC সার্ভার UserServiceImplBase থেকে ইনহেরিট করে সার্ভিস অপারেশন ইমপ্লিমেন্ট করে।
  3. Client-Side Invocation:
    • gRPC ক্লায়েন্ট তৈরি করা হয় যা সার্ভিসের স্টাব ব্যবহার করে সরাসরি RPC কল করে।

উপসংহার

gRPC এবং Protobuf ব্যবহার করে Spring Boot-এ ডেটা পাঠানো খুবই কার্যকর, বিশেষত মাইক্রোসার্ভিস আর্কিটেকচারে। এটি high-performance, type-safe, এবং platform-independent সমাধান প্রদান করে। Protobuf ডেটার আকার ছোট করে এবং দ্রুত প্রসেসিং নিশ্চিত করে, যা gRPC-এর পারফরম্যান্স বাড়ায়।

Content added By

উদাহরণ সহ gRPC Client Integration

96
96

gRPC (Google Remote Procedure Call) একটি হাই-পারফরম্যান্স RPC ফ্রেমওয়ার্ক যা Protocol Buffers (protobuf) ব্যবহার করে। Spring Boot-এর মাধ্যমে gRPC সার্ভিসগুলোর সাথে ক্লায়েন্ট ইন্টিগ্রেশন সহজেই করা যায়।


gRPC Client Integration এর ধাপসমূহ:

১. প্রয়োজনীয় ডিপেন্ডেন্সি যুক্ত করা

Maven ব্যবহার করলে pom.xml এ প্রয়োজনীয় ডিপেন্ডেন্সি যুক্ত করুন।

<dependencies>
    <!-- gRPC -->
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-netty-shaded</artifactId>
        <version>1.57.2</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
        <version>1.57.2</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
        <version>1.57.2</version>
    </dependency>

    <!-- Protocol Buffers -->
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.23.4</version>
    </dependency>
</dependencies>

Protobuf প্লাগইন:

Maven এর জন্য Protobuf প্লাগইন যোগ করুন:

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.7.0</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <configuration>
                <protocArtifact>com.google.protobuf:protoc:3.23.4:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.57.2:exe:${os.detected.classifier}</pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

২. Proto ফাইল তৈরি করা

src/main/proto/ ডিরেক্টরির মধ্যে .proto ফাইল তৈরি করুন।

example.proto:

syntax = "proto3";

package com.example.grpc;

service GreetingService {
    rpc greet (GreetingRequest) returns (GreetingResponse);
}

message GreetingRequest {
    string name = 1;
}

message GreetingResponse {
    string message = 1;
}

৩. Proto ফাইল কম্পাইল করা

Maven বিল্ড চালান:

mvn clean compile

এটি Java স্টাব ফাইল জেনারেট করবে: target/generated-sources/protobuf


৪. gRPC Client Configuration

Spring Boot-এ gRPC ক্লায়েন্ট কনফিগার করতে একটি Bean তৈরি করুন।

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GrpcClientConfig {

    @Bean
    public ManagedChannel managedChannel() {
        return ManagedChannelBuilder.forAddress("localhost", 9090) // gRPC server address
                .usePlaintext() // For development; disable SSL/TLS
                .build();
    }
}

৫. gRPC Client ব্যবহার

gRPC ক্লায়েন্ট স্টাব ব্যবহার করে সার্ভারে RPC কল করুন।

import com.example.grpc.GreetingRequest;
import com.example.grpc.GreetingResponse;
import com.example.grpc.GreetingServiceGrpc;
import io.grpc.ManagedChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GrpcClientService {

    private final GreetingServiceGrpc.GreetingServiceBlockingStub blockingStub;

    @Autowired
    public GrpcClientService(ManagedChannel managedChannel) {
        this.blockingStub = GreetingServiceGrpc.newBlockingStub(managedChannel);
    }

    public String sendGreeting(String name) {
        GreetingRequest request = GreetingRequest.newBuilder()
                .setName(name)
                .build();

        GreetingResponse response = blockingStub.greet(request);
        return response.getMessage();
    }
}

৬. Spring Boot Controller তৈরি

gRPC ক্লায়েন্ট কল করতে একটি REST এন্ডপয়েন্ট তৈরি করুন।

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GrpcClientController {

    @Autowired
    private GrpcClientService grpcClientService;

    @GetMapping("/greet")
    public String greet(@RequestParam String name) {
        return grpcClientService.sendGreeting(name);
    }
}

৭. gRPC সার্ভার তৈরি (ঐচ্ছিক)

আপনার ক্লায়েন্টের জন্য একটি gRPC সার্ভার প্রয়োজন হতে পারে। উদাহরণস্বরূপ:

import com.example.grpc.GreetingRequest;
import com.example.grpc.GreetingResponse;
import com.example.grpc.GreetingServiceGrpc;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;

@GRpcService
public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {

    @Override
    public void greet(GreetingRequest request, StreamObserver<GreetingResponse> responseObserver) {
        String message = "Hello, " + request.getName();
        GreetingResponse response = GreetingResponse.newBuilder()
                .setMessage(message)
                .build();

        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}

Spring Boot Application চালানো

  1. gRPC সার্ভার চালু করুন।
  2. Spring Boot ক্লায়েন্ট চালু করুন।
  3. ব্রাউজার বা Postman ব্যবহার করে REST API কল করুন:

    http://localhost:8080/greet?name=John
    

Response:

"Hello, John"

gRPC Client Integration এর সুবিধা

  1. হাই-পারফরম্যান্স: HTTP/2 ব্যবহার করে।
  2. কোড জেনারেশন: Proto ফাইল থেকে স্টাব জেনারেট করা হয়।
  3. কমপ্যাক্ট ডেটা ফরম্যাট: Protocol Buffers ডেটা সেরিয়ালাইজেশনে দক্ষ।
  4. বহুভাষার সমর্থন: gRPC বিভিন্ন ভাষায় সাপোর্ট করে।

উপসংহার

Spring Boot এর মাধ্যমে gRPC ক্লায়েন্ট তৈরি করা খুবই সহজ এবং এটি উচ্চ-পারফরম্যান্স ডিস্ট্রিবিউটেড সিস্টেমের জন্য আদর্শ। এটি REST-এর তুলনায় দ্রুত এবং দক্ষ সমাধান প্রদান করে।

Content added By
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion